package com.java.androidmarket.service.impl;

import com.java.androidmarket.dao.StockDao;
import com.java.androidmarket.entity.OrderGoods;
import com.java.androidmarket.entity.Stock;
import com.java.androidmarket.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * description:
 * author:张腾
 * date:2021-05-21
 */

@Service
public class StockServiceImpl implements StockService {

    @Autowired
    private StockDao stockDao;

    @Override
    public int getStock(int goodsId) {
        Stock stock = stockDao.getStock(goodsId);
        int realStock = stock.getStock()-stock.getSale()-stock.getLock();

        return realStock;
    }

    @Override
    public boolean checkStock(int goodsId,int wantNums) {
        Stock stock = stockDao.getStock(goodsId);
        int realStock = stock.getStock()-stock.getSale()-stock.getLock();

        if (wantNums>realStock){
            return false;
        }else{
            return true;
        }

    }

    @Override
    public void setLock(int goodsId, int lockNum) {
        stockDao.setLock(goodsId,lockNum);
    }

    public void reLock(int goodsId,int lockNum){
        stockDao.reLock(goodsId,lockNum);
    }

    @Override
    public void lockTosSale(OrderGoods orderGoods) {
        Stock stock = new Stock();
        stock.setGoodsId(orderGoods.getGoodsId()).setSale(orderGoods.getGoodsNum()).setLock(orderGoods.getGoodsNum());

        stockDao.lockTosSale(stock);
    }

    public Stock getS(int goodsId){
        Stock stock = stockDao.getStock(goodsId);
        return stock;
    }
}
